package org.jeecg.modules.online.config.service.p023a;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgform.consts.ExtendJsonKey;
import org.jeecg.modules.online.cgform.utils.CgformUtil;
import org.jeecg.modules.online.config.exception.DBException;
import org.jeecg.modules.online.config.p022d.ColumnMeta;
import org.jeecg.modules.online.config.p022d.DataBaseConst;
import org.jeecg.modules.online.config.service.DbTableHandleI;

/* renamed from: org.jeecg.modules.online.config.service.a.f */
/* loaded from: hibernate-re-3.7.1-RC.jar:org/jeecg/modules/online/config/service/a/f.class */
public class DbTablePostgresHandleImpl implements DbTableHandleI {
    @Override // org.jeecg.modules.online.config.service.DbTableHandleI
    public String getAddColumnSql(ColumnMeta columnMeta) {
        return " ADD COLUMN " + m8a(columnMeta) + ";";
    }

    @Override // org.jeecg.modules.online.config.service.DbTableHandleI
    public String getReNameFieldName(ColumnMeta columnMeta) {
        return " RENAME  COLUMN  " + columnMeta.getOldColumnName() + " to " + columnMeta.getColumnName() + ";";
    }

    @Override // org.jeecg.modules.online.config.service.DbTableHandleI
    public String getUpdateColumnSql(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) throws DBException {
        return m4c(cgformcolumnMeta, datacolumnMeta);
    }

    @Override // org.jeecg.modules.online.config.service.DbTableHandleI
    public String getSpecialHandle(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        String m3d = m3d(cgformcolumnMeta, datacolumnMeta);
        if (oConvertUtils.isNotEmpty(m3d)) {
            return "  ALTER  COLUMN   " + m3d + ";";
        }
        return null;
    }

    @Override // org.jeecg.modules.online.config.service.DbTableHandleI
    public String getMatchClassTypeByDataType(String dataType, int digits) {
        String str = "";
        if (DataBaseConst.f581l.equalsIgnoreCase(dataType)) {
            str = "string";
        } else if ("double".equalsIgnoreCase(dataType)) {
            str = "double";
        } else if (dataType.contains("int")) {
            str = "int";
        } else if ("Date".equalsIgnoreCase(dataType)) {
            str = "date";
        } else if ("timestamp".equalsIgnoreCase(dataType)) {
            str = "datetime";
        } else if ("bytea".equalsIgnoreCase(dataType)) {
            str = "blob";
        } else if (ExtendJsonKey.f133f.equalsIgnoreCase(dataType)) {
            str = ExtendJsonKey.f133f;
        } else if (DataBaseConst.f574e.equalsIgnoreCase(dataType)) {
            str = "bigdecimal";
        } else if ("numeric".equalsIgnoreCase(dataType)) {
            str = "bigdecimal";
        }
        return str;
    }

    @Override // org.jeecg.modules.online.config.service.DbTableHandleI
    public String dropTableSQL(String tableName) {
        return " DROP TABLE IF EXISTS " + tableName + " ;";
    }

    @Override // org.jeecg.modules.online.config.service.DbTableHandleI
    public String getDropColumnSql(String fieldName) {
        return " DROP COLUMN " + fieldName + ";";
    }

    /* renamed from: a */
    private boolean m9a(String str, String str2) {
        return "int,double,bigdecimal".indexOf(str == null ? "" : str.toLowerCase()) >= 0 && "int,double,bigdecimal".indexOf(str2 == null ? "" : str2.toLowerCase()) >= 0;
    }

    /* renamed from: a */
    private String m7a(ColumnMeta columnMeta, ColumnMeta columnMeta2) {
        return getDropColumnSql(columnMeta2.getColumnName()) + (String.format("alter table %s", columnMeta.getTableName()) + getAddColumnSql(columnMeta));
    }

    /* renamed from: b */
    private String m5b(ColumnMeta columnMeta, ColumnMeta columnMeta2) {
        String isNullable = columnMeta.getIsNullable();
        String isNullable2 = columnMeta2.getIsNullable();
        String str = isNullable == null ? "Y" : isNullable;
        if (!str.equals(isNullable2 == null ? "Y" : isNullable2)) {
            String tableName = columnMeta.getTableName();
            String columnName = columnMeta.getColumnName();
            if ("Y".equals(str)) {
                return String.format("ALTER table %s ALTER COLUMN %s %s not null;", tableName, columnName, "drop");
            }
            if ("N".equals(str)) {
                return String.format("ALTER table %s ALTER COLUMN %s %s not null;", tableName, columnName, "set");
            }
            return "";
        }
        return "";
    }

    /* renamed from: c */
    private String m4c(ColumnMeta columnMeta, ColumnMeta columnMeta2) throws DBException {
        String str = "  ALTER  COLUMN   ";
        if ("string".equalsIgnoreCase(columnMeta.getColunmType())) {
            str = str + columnMeta.getColumnName() + "  type character varying(" + columnMeta.getColumnSize() + ") ";
        } else if ("date".equalsIgnoreCase(columnMeta.getColunmType())) {
            if (columnMeta2.getColunmType().toLowerCase().indexOf("date") >= 0) {
                str = str + columnMeta.getColumnName() + "  type date ";
            } else {
                str = m7a(columnMeta, columnMeta2);
            }
        } else if ("datetime".equalsIgnoreCase(columnMeta.getColunmType())) {
            if (columnMeta2.getColunmType().toLowerCase().indexOf("date") >= 0) {
                str = str + columnMeta.getColumnName() + "  type timestamp ";
            } else {
                str = m7a(columnMeta, columnMeta2);
            }
        } else if ("int".equalsIgnoreCase(columnMeta.getColunmType())) {
            if (m9a(columnMeta.getColunmType(), columnMeta2.getColunmType())) {
                str = str + columnMeta.getColumnName() + " type int4";
            } else {
                str = m7a(columnMeta, columnMeta2);
            }
        } else if ("double".equalsIgnoreCase(columnMeta.getColunmType())) {
            if (m9a(columnMeta.getColunmType(), columnMeta2.getColunmType())) {
                str = str + columnMeta.getColumnName() + " type  numeric(" + columnMeta.getColumnSize() + CgformUtil.f233E + columnMeta.getDecimalDigits() + ") ";
            } else {
                str = m7a(columnMeta, columnMeta2);
            }
        } else if ("BigDecimal".equalsIgnoreCase(columnMeta.getColunmType())) {
            if (m9a(columnMeta.getColunmType(), columnMeta2.getColunmType())) {
                str = str + columnMeta.getColumnName() + " type  decimal(" + columnMeta.getColumnSize() + CgformUtil.f233E + columnMeta.getDecimalDigits() + ") ";
            } else {
                str = m7a(columnMeta, columnMeta2);
            }
        } else if (ExtendJsonKey.f133f.equalsIgnoreCase(columnMeta.getColunmType())) {
            str = str + columnMeta.getColumnName() + " type text ";
        } else if ("blob".equalsIgnoreCase(columnMeta.getColunmType())) {
            throw new DBException("blob类型不可修改");
        }
        if (StringUtils.isNotEmpty(columnMeta.getFieldDefault())) {
            str = str + ",  ALTER  COLUMN   " + columnMeta.getColumnName() + " set DEFAULT " + columnMeta.getFieldDefault() + " ";
        } else if (StringUtils.isNotEmpty(columnMeta2.getFieldDefault())) {
            str = str + ",  ALTER  COLUMN   " + columnMeta.getColumnName() + " DROP DEFAULT ";
        }
        if (!str.endsWith(";")) {
            str = str + ";";
        }
        return str + m5b(columnMeta, columnMeta2);
    }

    /* renamed from: d */
    private String m3d(ColumnMeta columnMeta, ColumnMeta columnMeta2) {
        String str = "";
        if (!columnMeta.m60a(columnMeta2)) {
            if ("string".equalsIgnoreCase(columnMeta.getColunmType())) {
                str = columnMeta.getColumnName() + (StringUtils.isNotEmpty(columnMeta.getFieldDefault()) ? " SET DEFAULT " + columnMeta.getFieldDefault() : " DROP DEFAULT");
            } else if ("date".equalsIgnoreCase(columnMeta.getColunmType()) || "datetime".equalsIgnoreCase(columnMeta.getColunmType())) {
                str = columnMeta.getColumnName() + (StringUtils.isNotEmpty(columnMeta.getFieldDefault()) ? " SET DEFAULT " + columnMeta.getFieldDefault() : " DROP DEFAULT");
            } else if ("int".equalsIgnoreCase(columnMeta.getColunmType())) {
                str = columnMeta.getColumnName() + (StringUtils.isNotEmpty(columnMeta.getFieldDefault()) ? " SET DEFAULT " + columnMeta.getFieldDefault() : " DROP DEFAULT");
            } else if ("double".equalsIgnoreCase(columnMeta.getColunmType())) {
                str = columnMeta.getColumnName() + (StringUtils.isNotEmpty(columnMeta.getFieldDefault()) ? " SET DEFAULT " + columnMeta.getFieldDefault() : " DROP DEFAULT");
            } else if ("bigdecimal".equalsIgnoreCase(columnMeta.getColunmType())) {
                str = columnMeta.getColumnName() + (StringUtils.isNotEmpty(columnMeta.getFieldDefault()) ? " SET DEFAULT " + columnMeta.getFieldDefault() : " DROP DEFAULT");
            } else if (ExtendJsonKey.f133f.equalsIgnoreCase(columnMeta.getColunmType())) {
                str = columnMeta.getColumnName() + (StringUtils.isNotEmpty(columnMeta.getFieldDefault()) ? " SET DEFAULT " + columnMeta.getFieldDefault() : " DROP DEFAULT");
            }
        }
        return str;
    }

    /* renamed from: a */
    private String m8a(ColumnMeta columnMeta) {
        String str = "";
        if ("string".equalsIgnoreCase(columnMeta.getColunmType())) {
            str = columnMeta.getColumnName() + " character varying(" + columnMeta.getColumnSize() + ") ";
        } else if ("date".equalsIgnoreCase(columnMeta.getColunmType())) {
            str = columnMeta.getColumnName() + " date ";
        } else if ("datetime".equalsIgnoreCase(columnMeta.getColunmType())) {
            str = columnMeta.getColumnName() + " timestamp ";
        } else if ("int".equalsIgnoreCase(columnMeta.getColunmType())) {
            str = columnMeta.getColumnName() + " int4";
        } else if ("double".equalsIgnoreCase(columnMeta.getColunmType())) {
            str = columnMeta.getColumnName() + " numeric(" + columnMeta.getColumnSize() + CgformUtil.f233E + columnMeta.getDecimalDigits() + ") ";
        } else if ("bigdecimal".equalsIgnoreCase(columnMeta.getColunmType())) {
            str = columnMeta.getColumnName() + " decimal(" + columnMeta.getColumnSize() + CgformUtil.f233E + columnMeta.getDecimalDigits() + ") ";
        } else if ("blob".equalsIgnoreCase(columnMeta.getColunmType())) {
            str = columnMeta.getColumnName() + " bytea ";
        } else if (ExtendJsonKey.f133f.equalsIgnoreCase(columnMeta.getColunmType())) {
            str = columnMeta.getColumnName() + " text ";
        }
        String str2 = str + (StringUtils.isNotEmpty(columnMeta.getFieldDefault()) ? " DEFAULT " + columnMeta.getFieldDefault() : " ");
        if ("N".equals(columnMeta.getIsNullable())) {
            str2 = str2 + " NOT NULL ";
        }
        return str2;
    }

    /* renamed from: b */
    private String m6b(ColumnMeta columnMeta) {
        String str = "";
        if ("string".equalsIgnoreCase(columnMeta.getColunmType())) {
            str = columnMeta.getColumnName() + " character varying(" + columnMeta.getColumnSize() + ") ";
        } else if ("date".equalsIgnoreCase(columnMeta.getColunmType())) {
            str = columnMeta.getColumnName() + " date ";
        } else if ("datetime".equalsIgnoreCase(columnMeta.getColunmType())) {
            str = columnMeta.getColumnName() + " timestamp ";
        } else if ("int".equalsIgnoreCase(columnMeta.getColunmType())) {
            str = columnMeta.getColumnName() + " int(" + columnMeta.getColumnSize() + ") ";
        } else if ("double".equalsIgnoreCase(columnMeta.getColunmType())) {
            str = columnMeta.getColumnName() + " numeric(" + columnMeta.getColumnSize() + CgformUtil.f233E + columnMeta.getDecimalDigits() + ") ";
        }
        return str;
    }

    @Override // org.jeecg.modules.online.config.service.DbTableHandleI
    public String getCommentSql(ColumnMeta columnMeta) {
        return "COMMENT ON COLUMN " + columnMeta.getTableName() + "." + columnMeta.getColumnName() + " IS '" + columnMeta.getComment() + CgformUtil.f231C;
    }

    @Override // org.jeecg.modules.online.config.service.DbTableHandleI
    public String dropIndexs(String indexName, String tableName) {
        return "DROP INDEX " + indexName;
    }

    @Override // org.jeecg.modules.online.config.service.DbTableHandleI
    public String countIndex(String indexName, String tableName) {
        return "SELECT count(*) FROM pg_indexes WHERE indexname = '" + indexName + "' and tablename = '" + tableName + CgformUtil.f231C;
    }
}
